package com.xcc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.inspur.humantask.service.SyncService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@RestController
public class TestController {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@RequestMapping("upload")
	public void testController() {
		
		RestTemplate postIPNetworkarea= new RestTemplate();
		String url="http://10.129.93.3:20000/postSend";
	//	String url="http://10.154.6.88:20000/postSend";
		HttpHeaders headers = new HttpHeaders();
	//	headers.set("Content-Type", "application/json");
	//	headers.set("Accept", " */*");
		StringBuffer resultBuffer = new StringBuffer();
/*
	      String bigDataSql = "select '1' as loc_flag,'3' as time_flag ,'江苏' as provincename,'苏州市' as cityname, CONCAT(kpi_time,'-01 00:00:00') as starttime,"
	      		+ " '苏州研发中心' as sourcename, kpi_code as indicator_code,kpi_value as indicator_value from scidc_rm.jt_rate_sb where kpi_time =\'"+"2019-09"+"\'";
	      
	      List bigDataList=new ArrayList() ;
	      
	      bigDataList = jdbcTemplate.queryForList(bigDataSql);
	      JSONArray js = JSONArray.fromObject(bigDataList);*/
//	String body=js.toString();
		
	String	body="[{\"loc_flag\":\"1\",\"time_flag\":\"3\",\",\"sourcename\":\"苏州研发";
		/*try {
			 HttpPost request0 = new HttpPost(url);
			 request0.setHeader("Content-type", "application/json; charset=utf-8");
			 HttpClient httpclient0 = HttpClients.createDefault();
			 HttpResponse response0;
			 StringEntity se = new StringEntity(body ,"UTF-8");
			 request0.setEntity(se);
			response0 = httpclient0.execute(request0);
		//	 String retSrc0 = EntityUtils.toString(response0.getEntity());
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		
		
		
		
		
		
		
		
		
		
		HttpEntity<String> requestEntity = new HttpEntity<String>(body, headers);
		ResponseEntity<String> response= postIPNetworkarea.postForEntity(url, requestEntity, String.class);
	 
	//	sendPost(url,body);
		
		
	}
	
	@Autowired
	SyncService mSyncService;
	
	@RequestMapping("key")
	public Object testKey()   {
		/*String humanTaskId="3182897";
		String bussinKeySql= "select KEY_ from  scidc.act_re_procdef WHERE ID_=(select PROC_DEF_ID_ from scidc.ACT_HI_PROCINST  where PROC_INST_ID_= "+humanTaskId+")";
		System.out.println(bussinKeySql);
		String key=jdbcTemplate.queryForList(bussinKeySql, String.class).get(0);
		
		String keyConfig="select * from scidc.t_dic where dic_type='"+key+"'";
		System.out.println(keyConfig);
		List<Map<String, Object>> mconfigs=jdbcTemplate.queryForList(keyConfig) ;
		String formJsonNew="{\"demandTitle\":\"物理机222\",\"priorityCode\":\"teji\",\"destPeopleId\":\"883\",\"destPeople\":\"张艳红\",\"destDepartment\":\"运营支撑部/技术二组\",\"destEmail\":\"zhangyanhong@cmss.chinamobile.com\",\"destPhone\":\"18896725390\",\"amount\":\"2\",\"cost\":\"0.0033\",\"vmList\":\"[{\\\"rowId\\\":\\\"\\\",\\\"area\\\":\\\"无锡\\\",\\\"resourcePool\\\":\\\"研发区\\\",\\\"purposeType\\\":\\\"项目\\\",\\\"name\\\":\\\"江西移动2016年业务支撑分布式数据库软件维保项目\\\",\\\"code\\\":\\\"R201685554\\\",\\\"kernelNumber\\\":\\\"2\\\",\\\"memory\\\":\\\"2\\\",\\\"hardDisk\\\":\\\"2\\\",\\\"demandDes\\\":\\\"222\\\",\\\"focusType\\\":\\\"1\\\",\\\"useScene\\\":\\\"产品开发\\\",\\\"purposeClassify\\\":\\\"持续集成\\\",\\\"useType\\\":\\\"自用\\\",\\\"applyNumber\\\":\\\"2\\\",\\\"user\\\":\\\"张艳红\\\",\\\"userId\\\":\\\"883\\\",\\\"userDeptId\\\":\\\"1113\\\",\\\"userMobile\\\":\\\"18896725390\\\",\\\"mailbox\\\":\\\"zhangyanhong@cmss.chinamobile.com\\\",\\\"purpose\\\":\\\"22\\\",\\\"endTime\\\":\\\"2019-12-19\\\"}]\",\"price\":\"0.02\",\"applyUserRole\":\"false\",\"isOrg\":\"true\",\"deptId\":\"1113\",\"dealProcessDatas\":[]}";
		String formJsonOld="{\"demandTitle\":\"物理机222\",\"priorityCode\":\"teji\",\"destPeopleId\":\"883\",\"destPeople\":\"张艳红\",\"destDepartment\":\"运营支撑部/技术二组\",\"destEmail\":\"zhangyanhong@cmss.chinamobile.com\",\"destPhone\":\"18896725390\",\"amount\":\"2\",\"cost\":\"0.0033\",\"vmList\":\"[{\\\"rowId\\\":\\\"\\\",\\\"area\\\":\\\"无锡\\\",\\\"resourcePool\\\":\\\"研发区\\\",\\\"purposeType\\\":\\\"项目\\\",\\\"name\\\":\\\"江西移动2016年业务支撑分布式数据库软件维保项目\\\",\\\"code\\\":\\\"R201685554\\\",\\\"kernelNumber\\\":\\\"2\\\",\\\"memory\\\":\\\"2\\\",\\\"hardDisk\\\":\\\"2\\\",\\\"demandDes\\\":\\\"222\\\",\\\"focusType\\\":\\\"1\\\",\\\"useScene\\\":\\\"产品开发\\\",\\\"purposeClassify\\\":\\\"持续集成\\\",\\\"useType\\\":\\\"自用\\\",\\\"applyNumber\\\":\\\"2\\\",\\\"user\\\":\\\"张艳红\\\",\\\"userId\\\":\\\"883\\\",\\\"userDeptId\\\":\\\"1113\\\",\\\"userMobile\\\":\\\"18896725390\\\",\\\"mailbox\\\":\\\"zhangyanhong@cmss.chinamobile.com\\\",\\\"purpose\\\":\\\"22\\\",\\\"endTime\\\":\\\"2019-12-19\\\"}]\",\"price\":\"0.02\",\"applyUserRole\":\"false\",\"isOrg\":\"true\",\"deptId\":\"1113\",\"dealProcessDatas\":[]}";
		
		JSONObject mFormJson = JSONObject.fromObject(formJsonNew);
		List<HashMap<String,String>> mTable=new ArrayList<>();
		
		
		for(int i=0;i<mconfigs.size();i++) {
			HashMap<String,String> index=new HashMap(); 
			index.put("name",  mFormJson.get(mconfigs.get(i).get("dic_value"))+"");
			index.put("newValue",  mFormJson.get(mconfigs.get(i).get("dic_key"))+"");
	//		index.put("field",  mFormJson.get(mconfigs.get(i).get("dic_key"))+"");
			 
			 
		 }
		
		
		return	mconfigs;*/
		return mSyncService.testKey("sadd", "", "");
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	 public  String sendPost(String url, String param) {
	        PrintWriter out = null;
	        BufferedReader in = null;
	        String result = "";
	        try {
	            URL realUrl = new URL(url);
	            // 打开和URL之间的连接
	            URLConnection conn = realUrl.openConnection();
	            // 设置通用的请求属性
	            conn.setRequestProperty("accept", "*/*");
	            conn.setRequestProperty("Content-Type", "application/json");
	            conn.setRequestProperty("connection", "Keep-Alive");
	            //conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
	            // 发送POST请求必须设置如下两行
	            conn.setDoOutput(true);
	            conn.setDoInput(true);
	            //1.获取URLConnection对象对应的输出流
	            out = new PrintWriter(conn.getOutputStream());
	            //2.中文有乱码的需要将PrintWriter改为如下
	            //out=new OutputStreamWriter(conn.getOutputStream(),"UTF-8")
	            // 发送请求参数
	            out.print(param);
	            // flush输出流的缓冲
	            out.flush();
	            // 定义BufferedReader输入流来读取URL的响应
	            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	            String line;
	            while ((line = in.readLine()) != null) {
	                result += line;
	            }
	        } catch (Exception e) {
	            System.out.println("发送 POST 请求出现异常！"+e);
	            e.printStackTrace();
	        }
	        //使用finally块来关闭输出流、输入流
	        finally{
	            try{
	                if(out!=null){
	                    out.close();
	                }
	                if(in!=null){
	                    in.close();
	                }
	            }
	            catch(IOException ex){
	                ex.printStackTrace();
	            }
	        }
	        System.out.println("post推送结果："+result);
	        return result;
	    }

	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	
}
